package com.tencent.mid.a;

import com.tencent.mid.util.Util;
import com.tencent.mid.util.g;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.GZIPOutputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class b
{
  private static DefaultHttpClient a = null;
  private static int b = 50000;

  public static f a(String paramString)
  {
    a = a();
    Util.logInfo("http get:" + paramString);
    HttpGet localHttpGet = new HttpGet(paramString);
    HttpHost localHttpHost = Util.getHttpProxy(d.a());
    StringBuilder localStringBuilder = new StringBuilder("proxy==");
    if (localHttpHost == null)
    {
      paramString = "null";
      Util.logInfo(paramString);
      if (localHttpHost == null)
        break label176;
      localHttpGet.addHeader("X-Online-Host", "pingmid.qq.com");
      localHttpGet.addHeader("Accept", "*/*");
      localHttpGet.removeHeaders("X-Content-Encoding");
    }
    while (true)
    {
      while (true)
      {
        localHttpGet.addHeader("Accept-Encoding", "gzip");
        paramString = a.execute(localHttpGet);
        int i = paramString.getStatusLine().getStatusCode();
        paramString = a(paramString);
        Util.logInfo("http get response code:" + i + " ,data:" + paramString);
        return new f(i, paramString);
        paramString = localHttpHost.getHostName();
      }
      label176: a.getParams().removeParameter("http.route.default-proxy");
    }
  }

  public static f a(String paramString1, String paramString2)
  {
    int i;
    a = a();
    Util.logInfo("[" + paramString1 + "]Send request(" + paramString2.length() + "bytes):" + paramString2);
    Object localObject1 = new HttpPost(paramString1);
    ((HttpPost)localObject1).addHeader("Accept-Encoding", "gzip");
    ((HttpPost)localObject1).setHeader("Connection", "Keep-Alive");
    ((HttpPost)localObject1).removeHeaders("Cache-Control");
    HttpHost localHttpHost = Util.getHttpProxy(d.a());
    Object localObject2 = new StringBuilder("proxy==");
    if (localHttpHost == null)
    {
      paramString1 = "null";
      Util.logInfo(paramString1);
      if (localHttpHost == null)
        break label342;
      ((HttpPost)localObject1).addHeader("X-Online-Host", "pingmid.qq.com");
      ((HttpPost)localObject1).addHeader("Accept", "*/*");
      ((HttpPost)localObject1).addHeader("Content-Type", "json");
      localObject2 = new ByteArrayOutputStream();
      paramString1 = paramString2.getBytes("UTF-8");
      i = paramString1.length;
      if (paramString2.length() >= 256)
        break label371;
      if (localHttpHost != null)
        break label359;
      ((HttpPost)localObject1).addHeader("Content-Encoding", "rc4");
      ((ByteArrayOutputStream)localObject2).close();
      ((HttpPost)localObject1).setEntity(new ByteArrayEntity(g.a(paramString1)));
      paramString2 = a.execute((HttpUriRequest)localObject1);
      localObject1 = paramString2.getEntity();
      i = paramString2.getStatusLine().getStatusCode();
      long l = ((HttpEntity)localObject1).getContentLength();
      Util.logInfo("recv response status code:" + i + ", content length:" + l);
      paramString1 = null;
      if (l > 0L)
        break label478;
      EntityUtils.toString((HttpEntity)localObject1);
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            Util.logInfo("recv response status code:" + i + ", content :" + paramString1);
            return new f(i, paramString1);
            paramString1 = localHttpHost.getHostName();
          }
          label342: a.getParams().removeParameter("http.route.default-proxy");
        }
        label359: ((HttpPost)localObject1).addHeader("X-Content-Encoding", "rc4");
      }
      if (localHttpHost == null)
        label371: ((HttpPost)localObject1).addHeader("Content-Encoding", "rc4,gzip");
      while (true)
      {
        while (true)
        {
          ((ByteArrayOutputStream)localObject2).write(new byte[4]);
          paramString2 = new GZIPOutputStream((OutputStream)localObject2);
          paramString2.write(paramString1);
          paramString2.close();
          paramString1 = ((ByteArrayOutputStream)localObject2).toByteArray();
          ByteBuffer.wrap(paramString1, 0, 4).putInt(i);
          Util.logInfo("before Gzip:" + i + " bytes, after Gzip:" + paramString1.length + " bytes");
        }
        ((HttpPost)localObject1).addHeader("X-Content-Encoding", "rc4,gzip");
      }
      label478: paramString1 = a(paramString2);
    }
  }

  public static String a(Map paramMap)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    if ((paramMap != null) && (paramMap.size() != 0))
    {
      Iterator localIterator = paramMap.entrySet().iterator();
      int i = 0;
      if (localIterator.hasNext())
      {
        Map.Entry localEntry = (Map.Entry)localIterator.next();
        if (i == 0);
        for (paramMap = "?"; ; paramMap = "&")
          while (true)
          {
            localStringBuilder.append(paramMap);
            localStringBuilder.append((String)localEntry.getKey());
            localStringBuilder.append("=");
            localStringBuilder.append((String)localEntry.getValue());
            i += 1;
          }
      }
    }
    return localStringBuilder.toString();
  }

  private static String a(HttpResponse paramHttpResponse)
  {
    Object localObject = paramHttpResponse.getEntity().getContent();
    DataInputStream localDataInputStream = new DataInputStream((InputStream)localObject);
    byte[] arrayOfByte = new byte[(int)paramHttpResponse.getEntity().getContentLength()];
    localDataInputStream.readFully(arrayOfByte);
    ((InputStream)localObject).close();
    localDataInputStream.close();
    localObject = paramHttpResponse.getFirstHeader("Content-Encoding");
    paramHttpResponse = arrayOfByte;
    if (localObject != null)
      if (!(((Header)localObject).getValue().equalsIgnoreCase("gzip,rc4")))
        break label98;
    for (paramHttpResponse = g.b(Util.deocdeGZipContent(arrayOfByte)); ; paramHttpResponse = g.b(arrayOfByte))
      do
      {
        while (true)
        {
          return new String(paramHttpResponse, "UTF-8");
          if (((Header)localObject).getValue().equalsIgnoreCase("rc4,gzip"))
            label98: paramHttpResponse = Util.deocdeGZipContent(g.b(arrayOfByte));
          if (!(((Header)localObject).getValue().equalsIgnoreCase("gzip")))
            break;
          paramHttpResponse = Util.deocdeGZipContent(arrayOfByte);
        }
        paramHttpResponse = arrayOfByte;
      }
      while (!(((Header)localObject).getValue().equalsIgnoreCase("rc4")));
  }

  // ERROR //
  public static DefaultHttpClient a()
  {
    // Byte code:
    //   0: ldc 2
    //   2: monitorenter
    //   3: getstatic 12	com/tencent/mid/a/b:a	Lorg/apache/http/impl/client/DefaultHttpClient;
    //   6: ifnonnull +245 -> 251
    //   9: invokestatic 330	com/tencent/mid/api/MidService:isEnableDebug	()Z
    //   12: istore_0
    //   13: iload_0
    //   14: ifeq +77 -> 91
    //   17: ldc_w 332
    //   20: invokestatic 338	java/util/logging/Logger:getLogger	(Ljava/lang/String;)Ljava/util/logging/Logger;
    //   23: getstatic 344	java/util/logging/Level:FINER	Ljava/util/logging/Level;
    //   26: invokevirtual 348	java/util/logging/Logger:setLevel	(Ljava/util/logging/Level;)V
    //   29: ldc_w 350
    //   32: invokestatic 338	java/util/logging/Logger:getLogger	(Ljava/lang/String;)Ljava/util/logging/Logger;
    //   35: getstatic 344	java/util/logging/Level:FINER	Ljava/util/logging/Level;
    //   38: invokevirtual 348	java/util/logging/Logger:setLevel	(Ljava/util/logging/Level;)V
    //   41: ldc_w 352
    //   44: ldc_w 354
    //   47: invokestatic 360	java/lang/System:setProperty	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   50: pop
    //   51: ldc_w 362
    //   54: ldc_w 364
    //   57: invokestatic 360	java/lang/System:setProperty	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   60: pop
    //   61: ldc_w 366
    //   64: ldc_w 368
    //   67: invokestatic 360	java/lang/System:setProperty	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   70: pop
    //   71: ldc_w 370
    //   74: ldc_w 368
    //   77: invokestatic 360	java/lang/System:setProperty	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   80: pop
    //   81: ldc_w 372
    //   84: ldc_w 368
    //   87: invokestatic 360	java/lang/System:setProperty	(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
    //   90: pop
    //   91: new 374	org/apache/http/params/BasicHttpParams
    //   94: dup
    //   95: invokespecial 375	org/apache/http/params/BasicHttpParams:<init>	()V
    //   98: astore_1
    //   99: aload_1
    //   100: getstatic 15	com/tencent/mid/a/b:b	I
    //   103: i2l
    //   104: invokestatic 381	org/apache/http/conn/params/ConnManagerParams:setTimeout	(Lorg/apache/http/params/HttpParams;J)V
    //   107: aload_1
    //   108: new 383	org/apache/http/conn/params/ConnPerRouteBean
    //   111: dup
    //   112: bipush 20
    //   114: invokespecial 386	org/apache/http/conn/params/ConnPerRouteBean:<init>	(I)V
    //   117: invokestatic 390	org/apache/http/conn/params/ConnManagerParams:setMaxConnectionsPerRoute	(Lorg/apache/http/params/HttpParams;Lorg/apache/http/conn/params/ConnPerRoute;)V
    //   120: aload_1
    //   121: bipush 100
    //   123: invokestatic 394	org/apache/http/conn/params/ConnManagerParams:setMaxTotalConnections	(Lorg/apache/http/params/HttpParams;I)V
    //   126: aload_1
    //   127: getstatic 400	org/apache/http/HttpVersion:HTTP_1_1	Lorg/apache/http/HttpVersion;
    //   130: invokestatic 406	org/apache/http/params/HttpProtocolParams:setVersion	(Lorg/apache/http/params/HttpParams;Lorg/apache/http/ProtocolVersion;)V
    //   133: aload_1
    //   134: ldc 164
    //   136: invokestatic 410	org/apache/http/params/HttpProtocolParams:setContentCharset	(Lorg/apache/http/params/HttpParams;Ljava/lang/String;)V
    //   139: aload_1
    //   140: iconst_0
    //   141: invokestatic 416	org/apache/http/params/HttpConnectionParams:setStaleCheckingEnabled	(Lorg/apache/http/params/HttpParams;Z)V
    //   144: aload_1
    //   145: iconst_1
    //   146: invokestatic 421	org/apache/http/client/params/HttpClientParams:setRedirecting	(Lorg/apache/http/params/HttpParams;Z)V
    //   149: aload_1
    //   150: getstatic 15	com/tencent/mid/a/b:b	I
    //   153: invokestatic 424	org/apache/http/params/HttpConnectionParams:setSoTimeout	(Lorg/apache/http/params/HttpParams;I)V
    //   156: aload_1
    //   157: getstatic 15	com/tencent/mid/a/b:b	I
    //   160: invokestatic 427	org/apache/http/params/HttpConnectionParams:setConnectionTimeout	(Lorg/apache/http/params/HttpParams;I)V
    //   163: aload_1
    //   164: iconst_1
    //   165: invokestatic 430	org/apache/http/params/HttpConnectionParams:setTcpNoDelay	(Lorg/apache/http/params/HttpParams;Z)V
    //   168: new 432	org/apache/http/conn/scheme/SchemeRegistry
    //   171: dup
    //   172: invokespecial 433	org/apache/http/conn/scheme/SchemeRegistry:<init>	()V
    //   175: astore_2
    //   176: aload_2
    //   177: new 435	org/apache/http/conn/scheme/Scheme
    //   180: dup
    //   181: ldc_w 437
    //   184: invokestatic 443	org/apache/http/conn/scheme/PlainSocketFactory:getSocketFactory	()Lorg/apache/http/conn/scheme/PlainSocketFactory;
    //   187: bipush 80
    //   189: invokespecial 446	org/apache/http/conn/scheme/Scheme:<init>	(Ljava/lang/String;Lorg/apache/http/conn/scheme/SocketFactory;I)V
    //   192: invokevirtual 450	org/apache/http/conn/scheme/SchemeRegistry:register	(Lorg/apache/http/conn/scheme/Scheme;)Lorg/apache/http/conn/scheme/Scheme;
    //   195: pop
    //   196: new 80	org/apache/http/impl/client/DefaultHttpClient
    //   199: dup
    //   200: new 452	org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager
    //   203: dup
    //   204: aload_1
    //   205: aload_2
    //   206: invokespecial 455	org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager:<init>	(Lorg/apache/http/params/HttpParams;Lorg/apache/http/conn/scheme/SchemeRegistry;)V
    //   209: aload_1
    //   210: invokespecial 458	org/apache/http/impl/client/DefaultHttpClient:<init>	(Lorg/apache/http/conn/ClientConnectionManager;Lorg/apache/http/params/HttpParams;)V
    //   213: putstatic 12	com/tencent/mid/a/b:a	Lorg/apache/http/impl/client/DefaultHttpClient;
    //   216: invokestatic 49	com/tencent/mid/a/d:a	()Landroid/content/Context;
    //   219: invokestatic 53	com/tencent/mid/util/Util:getHttpProxy	(Landroid/content/Context;)Lorg/apache/http/HttpHost;
    //   222: astore_1
    //   223: getstatic 12	com/tencent/mid/a/b:a	Lorg/apache/http/impl/client/DefaultHttpClient;
    //   226: invokevirtual 120	org/apache/http/impl/client/DefaultHttpClient:getParams	()Lorg/apache/http/params/HttpParams;
    //   229: ldc 122
    //   231: aload_1
    //   232: invokeinterface 462 3 0
    //   237: pop
    //   238: getstatic 12	com/tencent/mid/a/b:a	Lorg/apache/http/impl/client/DefaultHttpClient;
    //   241: new 464	com/tencent/mid/a/c
    //   244: dup
    //   245: invokespecial 465	com/tencent/mid/a/c:<init>	()V
    //   248: invokevirtual 471	org/apache/http/impl/client/AbstractHttpClient:setKeepAliveStrategy	(Lorg/apache/http/conn/ConnectionKeepAliveStrategy;)V
    //   251: invokestatic 49	com/tencent/mid/a/d:a	()Landroid/content/Context;
    //   254: invokestatic 53	com/tencent/mid/util/Util:getHttpProxy	(Landroid/content/Context;)Lorg/apache/http/HttpHost;
    //   257: astore_1
    //   258: getstatic 12	com/tencent/mid/a/b:a	Lorg/apache/http/impl/client/DefaultHttpClient;
    //   261: invokevirtual 120	org/apache/http/impl/client/DefaultHttpClient:getParams	()Lorg/apache/http/params/HttpParams;
    //   264: ldc 122
    //   266: aload_1
    //   267: invokeinterface 462 3 0
    //   272: pop
    //   273: getstatic 12	com/tencent/mid/a/b:a	Lorg/apache/http/impl/client/DefaultHttpClient;
    //   276: astore_1
    //   277: ldc 2
    //   279: monitorexit
    //   280: aload_1
    //   281: areturn
    //   282: astore_1
    //   283: aload_1
    //   284: invokevirtual 474	java/lang/Throwable:printStackTrace	()V
    //   287: goto -36 -> 251
    //   290: astore_1
    //   291: ldc 2
    //   293: monitorexit
    //   294: aload_1
    //   295: athrow
    //   296: astore_1
    //   297: goto -206 -> 91
    //
    // Exception table:
    //   from	to	target	type
    //   238	251	282	java/lang/Throwable
    //   3	13	290	finally
    //   17	91	290	finally
    //   91	238	290	finally
    //   238	251	290	finally
    //   251	277	290	finally
    //   283	287	290	finally
    //   17	91	296	java/lang/Throwable
  }

  public static void b()
  {
    try
    {
      if (a != null)
      {
        a.getConnectionManager().shutdown();
        a = null;
        Util.logInfo("close http client.");
      }
      return;
    }
    catch (Throwable localThrowable)
    {
      Util.logWarn(localThrowable);
    }
  }
}